# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
  def relative_time(from_time)
    from_time = from_time.to_time if from_time.respond_to?(:to_time)
    distance_in_minutes = (((Time.now - from_time).abs)/60).round

    case distance_in_minutes
      when 0..1           then (distance_in_minutes == 0) ? '< 1 min ago' : '1 min ago'
      when 2..45          then "#{distance_in_minutes} mins ago"
      when 46..90         then '1 hr ago'
      when 90..1049       then "#{(distance_in_minutes.to_f / 60.0).round} hrs ago"
      when 1050..2159     then "1 day ago"
      when 2160..8640     then "#{(distance_in_minutes / 1440.0).round} days ago"
      when 8641..10080    then '1 week ago'
      else
        from_time.strftime from_time.year == Time.now.year ? '%b %d' : '%b %d, %Y'
    end
  end
end
